package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.cache.BannedUserCache;
import com.conversationboard.cache.RecentMessagesList;
import com.conversationboard.cache.ThreadCache;
import com.conversationboard.cache.ThreadTitleCache;
import com.conversationboard.cache.UserCache;
import com.conversationboard.cache.VoterCache;
import com.conversationboard.cache.banvote.BanVoteCache;
import com.conversationboard.cache.newanswers.INewAnswersCache;
import com.conversationboard.cache.newanswers.NewAnswersCaches;
import com.conversationboard.cache.readcounter.ReadCounterCache;
import com.conversationboard.fulltext.search.Search;
import com.conversationboard.model.Board;
import com.conversationboard.model.Boards;
import com.conversationboard.model.User;
import com.conversationboard.site.Site;

@WebServlet(name = "AdminCacheSizesControllerServlet", urlPatterns = "/Admin/AdminCacheSizesControllerServlet")
public class AdminCacheSizesControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -3808465996758339907L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		try {

			User user = User.get(request.getUserPrincipal().getName());

			if (!user.isSiteAdmin()) {
				return;
			}

			request.setAttribute("voterCacheSize", VoterCache.getSizeInKilobytes());
			request.setAttribute("voterCacheElements", VoterCache.size());
			request.setAttribute("userCacheSize", UserCache.getSizeInKilobytes());
			request.setAttribute("userCacheElements", UserCache.size());
			request.setAttribute("threadTitleCacheSize", ThreadTitleCache.getSizeInKilobytes());
			request.setAttribute("threadTitleCacheElements", ThreadTitleCache.size());
			request.setAttribute("threadCacheSize", ThreadCache.getInstance().getSizeInKilobytes());
			request.setAttribute("threadCacheElements", ThreadCache.getInstance().size());
			request.setAttribute("recentMessageListSize", RecentMessagesList.getSizeInKilobytes());
			request.setAttribute("recentMessageListElements", RecentMessagesList.size());
			request.setAttribute("bannedUserCacheSize", BannedUserCache.getSizeInKilobytes());
			request.setAttribute("bannedUserCacheElements", BannedUserCache.size());
			request.setAttribute("maxMemory", Runtime.getRuntime().maxMemory());
			request.setAttribute("totalMemory", Runtime.getRuntime().totalMemory());
			request.setAttribute("freeMemory", Runtime.getRuntime().freeMemory());
			request.setAttribute("readCounterCacheSize", ReadCounterCache.getInstance().getSizeInKilobytes());
			request.setAttribute("readCounterCacheElements", ReadCounterCache.getInstance().size());

			Map<String, int[]> newAnswersCaches = new HashMap<String, int[]>();

			List<Board> boards = Boards.getAllBoards();

			int totalNewAnswersCacheSize = 0;
			int totalNewAnswersCacheElements = 0;

			for (Board board : boards) {
				INewAnswersCache cache = NewAnswersCaches.getInstance(board.getBoardId());
				int cacheSize = cache.getSizeInKilobytes();
				totalNewAnswersCacheSize += cacheSize;
				totalNewAnswersCacheElements += cache.size() + Site.getSiteNewAnswers().size();
				;

				int[] sizeAndElements = new int[2];
				sizeAndElements[0] = cacheSize;
				sizeAndElements[1] = cache.size();

				newAnswersCaches.put(board.getName(), sizeAndElements);
			}

			/* Add the site new answers cache */

			int cacheSize = Site.getSiteNewAnswers().getSizeInKilobytes();

			int[] siteSizeAndElements = new int[2];
			siteSizeAndElements[0] = cacheSize;
			siteSizeAndElements[1] = Site.getSiteNewAnswers().size();

			totalNewAnswersCacheSize += cacheSize;
			newAnswersCaches.put("Site New Answers Cache", siteSizeAndElements);

			request.setAttribute("totalNewAnswersCacheSize", totalNewAnswersCacheSize);
			request.setAttribute("totalNewAnswersCacheElements", totalNewAnswersCacheElements);
			request.setAttribute("newAnswersCaches", newAnswersCaches);
			request.setAttribute("banVoteCacheSize", BanVoteCache.getSizeInKilobytes());
			request.setAttribute("banVoteCacheElements", BanVoteCache.size());

			request.setAttribute("threadSearcherSize", Search.getThreadSearcherSizeInKilobytes());
			request.setAttribute("messageSearcherSize", Search.getMessageSearcherSizeInKilobytes());

			RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/Admin/cachesize.jsp");
			dispatcher.forward(request, response);

		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
